package com.sunrun.sunrunframwork.uibase;

import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;

import com.sunrun.sunrunframwork.R;
import com.sunrun.sunrunframwork.BaseApplication;
import com.sunrun.sunrunframwork.http.BaseBean;
import com.sunrun.sunrunframwork.http.NetServer;
import com.sunrun.sunrunframwork.http.cache.NetSession;
import com.sunrun.sunrunframwork.http.intf.NetRequestHandler;
import com.sunrun.sunrunframwork.http.intf.UIUpdateHandler;
import com.sunrun.sunrunframwork.uibase.util.StatusBarUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @Activity总父类
 */
public  abstract class BaseActivity extends FragmentActivity implements UIUpdateHandler
{
    protected FragmentActivity _CONTEXT;
    Unbinder unbinder;

    protected abstract int getLayoutId();
    public abstract void onViewCreated(View view, @Nullable Bundle savedInstanceState) ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        dealLongTimeBackGround(savedInstanceState);
        super.onCreate(savedInstanceState);
        _CONTEXT = this;
        if(getLayoutId()!=0) {
            setContentView(getLayoutId());
        }
        BaseApplication.getInstance().addActivity(this);
        dealStatusMode();
        onViewCreated(getWindow().getDecorView(),savedInstanceState);
    }
    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    NetServer netServer;
    public NetServer getNetRequest() {
        if(netServer==null){
            netServer=new NetServer(this,this);
        }
        return netServer;
    }

    /**
     * 处理程序长时间后台的问题, 默认是关闭该Activity,子类若不想被关闭,将该方法置空
     */
    public void dealLongTimeBackGround(Bundle arg0) {
    }


    @Override
    protected void onDestroy() {
        BaseApplication.getInstance().removeActivity(this);
        _CONTEXT = null;
        unbindView();
        super.onDestroy();
    }

    @Override
    public void nofityUpdate(int requestCode, BaseBean bean) {

    }

    @Override
    public void nofityUpdate(int requestCode, float progress) {

    }

    @Override
    public void dealData(int requestCode, BaseBean bean) {

    }

    @Override
    public void loadFaild(int requestCode, BaseBean bean) {

    }

    @Override
    public void emptyData(int requestCode, BaseBean bean) {

    }

    @Override
    public void requestFinish() {

    }

    @Override
    public void requestCancel() {

    }

    @Override
    public void requestStart() {

    }

    public NetSession getSession() {
        return NetSession.instance(this);
    }

    //===========================================状态栏沉浸相关===========================================
    protected boolean ifTitleBarIsWhiteThenIWillSetBlackStatusBarOnNotCompatDevice() {
        return isStatusContentDark();
    }
    /**
     * 启用状态栏透明
     *
     * @return
     */
    protected boolean isTranslucent() {
        return getStatusColorInStyle() == Color.TRANSPARENT;
    }

    private int getStatusColorInStyle() {
        int[] attrs = new int[]{R.attr.colorPrimaryDark};
        TypedArray typedArray = obtainStyledAttributes(attrs);
        int backgroundResource = typedArray.getColor(0, Color.BLACK);
        typedArray.recycle();
        return backgroundResource;
    }

    protected boolean isStatusContentDark() {
        int colorPrimary = getStatusColorInStyle();
        return colorPrimary == Color.TRANSPARENT || colorPrimary == Color.WHITE;
    }

    protected void dealStatusMode(){
        if (isTranslucent()) {
            StatusBarUtil.transparencyBar(this);
        }
        if (isStatusContentDark()) {
            final int type = StatusBarUtil.StatusBarLightMode(this);
            if (type <= 0 && ifTitleBarIsWhiteThenIWillSetBlackStatusBarOnNotCompatDevice()) {
                StatusBarUtil.setStatusBarColor(this, android.R.color.black);
            }
        }
    }
    //========================================视图绑定相关================================================
    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        bindView();
    }

    private void bindView() {
        unbinder=ButterKnife.bind(this);
    }
    private void unbindView() {
        unbinder.unbind();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        bindView();
    }
    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        bindView();
    }
}
